﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using PPI.Entities;

namespace PPIControls.Pole
{
    public partial class PoleTree : UserControl
    {
        #region Events

        public event EventHandler<PoleTreeEventArgs> ClickedEditPole;
        private void OnClickedEditPole(object o, PoleTreeEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEditPole != null)
            {
                ClickedEditPole(o, e);
            }
        }

        public event EventHandler<PoleTreeEventArgs> ClickedDeletePole;
        private void OnClickedDeletetPole(object o, PoleTreeEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDeletePole != null)
            {
                ClickedDeletePole(o, e);
            }
        }


        public event EventHandler<PoleTreeEventArgs> ClickedEditImage;
        private void OnClickedEditImage(object o, PoleTreeEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEditImage != null)
            {
                ClickedEditImage(o, e);
            }
        }

        public event EventHandler<PoleTreeEventArgs> ClickedNewPole;
        private void OnClickedNewPole(object o, PoleTreeEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedNewPole != null)
            {
                ClickedNewPole(o, e);
            }
        }



        #endregion

        public bool AllowDragAndDrop
        {
            set { tree.AllowDrop = value; }
        }

        public void DisableDragAndDrop(bool isDisabled)
        {
            tree.AllowDrop = isDisabled;
        }

        private EBaseAsset m_visit;

        public PoleTree()
        {
            InitializeComponent();
        }

        public void FillNode(EBaseAsset visit)
        {
            m_visit = visit;

            Node createPole = new Node();
            createPole.Selectable = true;
            createPole.DataKey = new EVendor_Visit_XMissionStructures_Pole();
            createPole.Image = global::PPIControls.Properties.Resources.newPole;
            createPole.Text = "Visit Images";
            createPole.NodeClick += new System.EventHandler(this.nodePole_NodeClick);
            createPole.DragDropEnabled = false;
            


            List<EBaseAssetImage> visitImages = m_visit.Images.FindAll(delegate(EBaseAssetImage i) { return i.Pole.Id == Int32.MinValue; });
            foreach (EBaseAssetImage i in visitImages)
            {
                Node disasociateImage = new Node();
                disasociateImage.Selectable = true;
                disasociateImage.DataKey = i;
                disasociateImage.Text = ((EVendor_Visit_XMissionStructures_Image) i).Title;
                disasociateImage.Image = global::PPIControls.Properties.Resources.picture;
                disasociateImage.NodeClick += new System.EventHandler(this.nodePole_NodeClick);
                createPole.Nodes.Add(disasociateImage);
            }
            tree.Nodes.Add(createPole);

            foreach (EBasePole p in m_visit.Poles)
            {
                Node nodePole = new Node();
                nodePole.Text = p.Title;
                nodePole.DataKey = p;
                nodePole.DragDropEnabled = false;
                nodePole.NodeClick += new EventHandler(nodePole_NodeClick);

                foreach (EBaseAssetImage i in p.Images)
                {
                    Node nodeImage = new Node();
                    nodeImage.Image = global::PPIControls.Properties.Resources.picture;
                    nodeImage.DataKey = i;
                    nodeImage.Text = ((EVendor_Visit_XMissionStructures_Image)i).Title;
                    nodeImage.NodeClick += new EventHandler(nodePole_NodeClick);
                    nodePole.Nodes.Add(nodeImage);
                }
                tree.Nodes.Add(nodePole);
            }

        }

        public void CleanTree()
        {
            tree.Nodes.Clear();
        }

        void nodePole_NodeClick(object sender, EventArgs e)
        {
            Int32 idImage = Int32.MinValue;
            Int32 idPole = Int32.MinValue;
            if(sender is Node)
            {
                Node tmpNode = (Node) sender;
                
                if(tmpNode.DataKey != null)
                {
                    if (tmpNode.DataKey is EBaseAssetImage)
                    {
                        idImage = ((EBaseAssetImage) tmpNode.DataKey).Id;
                        idPole = ((EBaseAssetImage) tmpNode.DataKey).Pole.Id;
                        OnClickedEditImage(this, new PoleTreeEventArgs(idPole,idImage));
                        return;
                    }
                }
            }
        }

        void tree_BeforeNodeDrop(object sender, DevComponents.AdvTree.TreeDragDropEventArgs e)
        {
            if (e.Action == eTreeAction.Mouse)
            {
                if (e.NewParentNode.DataKey is EBaseAssetImage && e.Node.DataKey is EBaseAssetImage)
                {
                    e.Cancel = true;
                }
                else
                {
                    if (e.NewParentNode.DataKey is EBasePole)
                    {
                        EBasePole tmpNewPole = (EBasePole)e.NewParentNode.DataKey;
                        EBaseAssetImage tmpImage = (EBaseAssetImage)e.Node.DataKey;
                        
                        tmpImage.Pole = tmpNewPole;
                        tmpNewPole.Images.Add(tmpImage);
                        if(e.OldParentNode != null)
                        {
                            EBasePole tmpOldPole = (EBasePole)e.OldParentNode.DataKey;
                            tmpOldPole.Images.Remove(tmpImage);
                        }
                    }
                }
            }
            
        }

        private void btiNewPole_Click(object sender, EventArgs e)
        {
            tree.DeselectNode(tree.SelectedNode,eTreeAction.Mouse);
            OnClickedNewPole(this, new PoleTreeEventArgs(Int32.MinValue)); 
        }

        private void btiEditPole_Click(object sender, EventArgs e)
        {
            Node selectedNode = tree.SelectedNode;
            if (selectedNode == null)
                return;
            if(selectedNode.DataKey is EBasePole)
            {
                Int32 idPole = ((EBasePole)selectedNode.DataKey).Id;
                OnClickedEditPole(this, new PoleTreeEventArgs(idPole));
            }
        }

        private void btiDeletePole_Click(object sender, EventArgs e)
        {
            Node selectedNode = tree.SelectedNode;
            if (selectedNode == null)
                return;
            if (selectedNode.DataKey is EBasePole)
            {
                Int32 idPole = ((EBasePole)selectedNode.DataKey).Id;
                if(selectedNode.HasChildNodes)
                {
                    for (int i = 0; i < selectedNode.Nodes.Count; i++ )
                    {
                        Node tmpImageNode = selectedNode.Nodes[i];
                        EBaseAssetImage tmpImage = (EBaseAssetImage) tmpImageNode.DataKey;
                        EBasePole tmpPole = (EBasePole) selectedNode.TreeControl.Nodes[0].DataKey;

                        tmpImage.Pole = tmpPole;
                        selectedNode.TreeControl.Nodes[0].Nodes.Add(tmpImageNode);


                    }
                }
                selectedNode.Remove();
                OnClickedDeletetPole(this, new PoleTreeEventArgs(idPole));

            }
        }
    }
}
